package com.example.notepad.diary

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.notepad.R
import java.text.SimpleDateFormat

class DiaryAdapter(private val context: AppCompatActivity, private val diaryEntries: List<DiaryEntry>) : ArrayAdapter<DiaryEntry>(context, 0, diaryEntries) {

    @SuppressLint("MissingInflatedId")
    override fun getView(position: Int, convertView: View?, parentView: ViewGroup): View {
        val inflater = context.layoutInflater
        val view = convertView ?: inflater.inflate(R.layout.diary_item, parentView, false)

        val diaryEntry = diaryEntries[position]
        val textView = view.findViewById<TextView>(R.id.diaryText)
        val timeView = view.findViewById<TextView>(R.id.savedTime)

        textView.text = diaryEntry.text
        timeView.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(diaryEntry.savedTime)

        // 设置点击事件
        view.setOnClickListener {
            val intent = Intent(context, EditDiaryActivity::class.java)
            intent.putExtra("diaryText", diaryEntry.text)
            intent.putExtra("diaryTime", SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(diaryEntry.savedTime))
            context.startActivity(intent)
        }

        return view
    }
}